package example

import (
	"gitee.com/851537/gbase"
	basewsclient "gitee.com/851537/gpkg/wsclient/base"
)

type Client struct {
	basewsclient.Base

	//消息处理观察者集线器
	MsgObserverHub *gbase.ObserverHub
}

// 注册观察者 - gpkg.basewsclient.IWsClient
func (c *Client) RegMsgObserver(observer gbase.IObserver) {
	c.GetMsgObserverHub().Register(observer.GetType(), observer)
}

func (c *Client) GetMsgObserverHub() *gbase.ObserverHub {
	return c.MsgObserverHub
}

func (c *Client) Start() {
	c.Base.Connect(c)
}

// 其它一些操作
//func (this *Client) Subscribe(data *okxreqmsg.Subscribe) (err error) {
//	return this.Send(this, NewSubscribeMsg(data).ToBytes())
//}

func NewClient(opts ...gbase.Option[basewsclient.IWsClient]) (obj *Client) {
	obj = new(Client)

	//初始化消息处理集线器
	obj.MsgObserverHub = gbase.NewObserverHub()

	//默认消息处理方法
	//obj.OnMessage = DefaultMsgHandler

	for _, v := range opts {
		v(obj)
	}

	//clients.Put(obj.apiKey, obj)

	return
}

// 加入观察者，适配时由该观察者处理消息,处理消息逻辑在DefaultMsgHandler
func WithObserver(observer gbase.IObserver) gbase.Option[*Client] {
	return func(obj *Client) {
		obj.MsgObserverHub.Register(observer.GetType(), observer)
	}
}
